package com.github.cosycode.imgtool.rule;

import com.github.cosycode.common.lang.BaseRuntimeException;
import com.github.cosycode.common.util.encode.MessageDigestUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.function.Function;
import java.util.function.UnaryOperator;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2022/8/7
 * </p>
 *
 * @author pengfchen
 * @since 1.0
 **/
public enum NameRule {

    MD5(filePath -> {
        try {
            return MessageDigestUtils.getMD5(new File(filePath));
        } catch (IOException e) {
            throw new BaseRuntimeException("计算文件 MD5 码失败, file: " + filePath, e);
        }
    }),
    DATE(f -> LocalDate.now().toString()),
    DATETIME(f -> LocalDateTime.now().toString());
    public final UnaryOperator<String> function;

    NameRule(UnaryOperator<String> function) {
        this.function = function;
    }
}
